package org.ariadne_eu.hcifetcher.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class AddCourseServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AddCourseServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String name = req.getParameter("name");
		String hashtag = req.getParameter("hashtag");
		log.log(Level.INFO, "Adding course named " + name + " with hashtag #"
						+ hashtag);
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Entity course = new Entity("Course");
		course.setProperty("name", name);
		course.setProperty("hashtag", hashtag);
		course.setProperty("datestamp", new Date());
		Key k = datastore.put(course);
		resp.getWriter().write("Adding course named " + name + " with hashtag #" + hashtag + ". Key: " + k);
		log.log(Level.INFO, "Adding course named " + name + " with hashtag #" + hashtag + ". Key: " + k);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req,resp);
	}

}
